package cn.flying.cloud.oauth.server.configuration.password;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;

import cn.flying.cloud.base.common.utils.Ft;

/**
 * 密码配置
 *
 * @author: admin
 * @date: 2024年06月14日 16:58
 * @version: 1.0
 */
@Configuration
public class CryptoConfiguration {

    private final String key = "878a3540cfc344d48353ab799d154623";

    /**
     * 加密器
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
//        使用 BCryptPasswordEncoder 密码编码器，该编码器会将随机产生的 salt 混入最终生成的密文中
//        return new BCryptPasswordEncoder();
        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence rawPassword) {
                return Ft.SM3.encrypt(rawPassword.toString(), key);
            }

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
//                return rawPassword.toString().equals(CryptHelper.decryptWithSm4(encodedPassword));
                return encodedPassword.matches(Ft.SM3.encrypt(rawPassword.toString(), key));
            }
        };
    }

}
